package com.fjd.controller;

import com.fjd.entity.MerchandiseInfo;
import com.fjd.result.Result;
import com.fjd.service.ProductService;
import com.fjd.vo.Category4VO;
import com.fjd.vo.MerchandiseInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopping/product")
@Slf4j
@Api(tags = "商品相关接口管理")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 根据商品id获取商品信息
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    @ApiOperation("根据商品id获取商品信息")
    public Result<MerchandiseInfoVO> getProduct(@PathVariable String productId){
        MerchandiseInfoVO merchandiseInfoVO = productService.getProductById(productId);
        return Result.success(merchandiseInfoVO);
    }

    @GetMapping("/menu")
    public Result<List<Category4VO>> getProductMenu(){
        List<Category4VO> category4VO = productService.getProductMenu();
        return Result.success(category4VO);
    }

    @GetMapping("/list_specification/{productId}")
    public Result<List<Category4VO>> getSpecification(@PathVariable String productId){
        List<Category4VO> category4VO = productService.getProductSpec(productId);
        return Result.success(category4VO);
    }

    @PostMapping("/addMerchandise")
    public Result addProduct(@RequestBody MerchandiseInfo merchandiseInfo){
        productService.addProduct(merchandiseInfo);
        return Result.success();
    }
    @PostMapping("/uploadimage")
    public Result uploadImage(@RequestBody Map<String, String> data) {
        String id = data.get("id");
        String imagePath = data.get("imagePath");

        productService.uploadImage(id, imagePath);
        return Result.success();
    }


    @PostMapping("/updatePrice")
    public Result updatePrice(@RequestParam String specId,@RequestParam Integer price){
        productService.updatePrice(specId, price);
        return Result.success();
    }

    @GetMapping("/listMerchandise")
    public Result<List<MerchandiseInfo>> getMerchandise(){
        List<MerchandiseInfo> merchandiseInfos = productService.getAllMerchandiseInfo();
        return Result.success(merchandiseInfos);
    }

    @PostMapping("/procUpdate")
    public Result editProduct(@RequestBody MerchandiseInfoVO merchandiseInfoVO){
        productService.editProduct(merchandiseInfoVO);
        return Result.success();
    }

}
